open FrGui

let plug widget xid =
	let plug = GWindow.plug ~window: xid ~show: true () in
	plug#add (uncoerce_widget widget)#widget

let socket start stop =
	let widget = GWindow.socket ()
	and data = ref None in
	ignore (widget#misc#connect#realize
		(fun () ->
			assert (!data = None);
			data := Some (start widget#xwindow)));
	(* not sure if window is still valid during unrealize or not *)
	ignore (widget#misc#connect#unrealize
		(fun () ->
			(match !data with
			| None -> assert false
			| Some d -> stop d);
			data := None));
	(* potential memory leak, if a lot of these are created
	   is there a better way, via GTK? *)
	at_exit (fun () ->
		match !data with
		| None -> ()
		| Some d -> stop d);
	coerce_widget (new simple_widget widget)
